package org.legzo.castorranker.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.commons.io.IOUtils;

public class ImageServlet extends javax.servlet.GenericServlet {

	private static final long serialVersionUID = -7114089586612862102L;
	
	private File parentDir = new File(System.getProperty("java.io.tmpdir"), "ranker");

	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		ServletOutputStream out = response.getOutputStream();
		String filename = request.getParameter("f");
		if (filename != null) {
			filename.replaceAll("\\.\\.", "");
			File reqFile = new File(parentDir, filename);
			if (reqFile.exists()) {
				response.setContentLength((int)reqFile.length());
				response.setContentType("image/png");
				FileInputStream fis = new FileInputStream(reqFile);
				IOUtils.copy(fis, out);
				IOUtils.closeQuietly(fis);
			}
		}
		IOUtils.closeQuietly(out);
		
	}
	
	
}
